$(function() {
  var donateWrap = $('#donateWrap');
  var donateContainer = $('#donateContainer');
  var donateList = $('#donateList');
  var qrBox = $('#qrBox');
  var imgBox = $('#imgBox');
  var count = 0;

  var donateType = {
    BTC: donateWrap.data('btcqr'),
    AliPay: donateWrap.data('alipayqr'),
    WeChat: donateWrap.data('wechatqr'),
    PayPal: donateWrap.data('paypal'),
  };
  var GitHub = donateWrap.data('github');

  function showQR(QR) {
    if (QR) {
      imgBox.attr('src', QR);
    }
    donateContainer.addClass('blur');
    qrBox.fadeIn(300, function() {
      imgBox.addClass('showQR');
    });
  }

  function hideQR() {
    imgBox.removeClass('showQR').addClass('hideQR');
    donateContainer.removeClass('blur');
    qrBox.fadeOut(300, function() {
      imgBox.removeClass('hideQR');
    });
  }

  function handleItemClick() {
    var thisID = $(this).attr('id');
    if (thisID === 'BTC') {
      showQR(donateType.BTC);
    }
    if (thisID === 'AliPay') {
      showQR(donateType.AliPay);
    }
    if (thisID === 'WeChat') {
      showQR(donateType.WeChat);
    }
    if (thisID === 'PayPal') {
      window.open(donateType.PayPal, '_blank');
    }
    if (thisID === 'github') {
      window.open(GitHub, '_blank');
    }
  }

  Object.keys(donateType).forEach(function(v) {
    if (donateType[v]) {
      count++;
      $('#' + v).show();
    }
  });

  if (count) {
    $('#ps').hide();
  }
  $('li', donateList).css('width', 100 / count + '%');
  $('li', donateList).on('click', handleItemClick);

  imgBox.on('click', hideQR);
});
